package cn.bluethink.eguan.core.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.bluethink.eguan.core.security.Token;
import cn.bluethink.eguan.core.service.EgCheckService;
import cn.bluethink.eguan.model.core.EgCheck;
import cn.bluethink.eguan.model.core.EgCheckStatus;
import cn.bluethink.eguan.model.core.EgPurview;
import cn.bluethink.eguan.result.ResponseMessage;
import cn.bluethink.eguan.result.ResponseResult;

/**
 * 审核
 * 
 * @author 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.3.0
 * @brief 驿馆+产品
 * @note 修订历史： 1.fanbl于2019/2/19设计并构建初始版本v1.0
 */
@CrossOrigin
@RestController
@RequestMapping("/eguan/v1.0/check")
public class EgCheckController {

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private EgCheckService checkService;

	/**
	 * 审核
	 * @param egCheck
	 * @return
	 * @throws Exception
	 */
	@Token(purviews = { EgPurview.OBJECT_CHECK })
	@PostMapping("/create")
	public ResponseResult<EgCheck> create(@RequestBody EgCheck egCheck) throws Exception {
		// 获取当前登录用户Id
		Long uid = (Long) request.getAttribute("userId");
       // 获取审核状态
		Integer status = egCheck.getStatus();
		if(status == null || status == EgCheckStatus.STATUS_PENDING) return new ResponseResult<>(ResponseMessage.PARAMETER_ERROR.getStatus(), "审核状态为待审核，请重新操作！");
		EgCheck check = checkService.check(egCheck, uid);
		if (check != null) return new ResponseResult<>(ResponseMessage.OK, check);
		return new ResponseResult<>(ResponseMessage.CHECK_FAIL);

	}

}
